package cn.com.ejiakanghu.service;

import cn.com.ejiakanghu.config.LocationContext;
import cn.com.ejiakanghu.core.utils.RespUtil;
import cn.com.ejiakanghu.core.vo.BaseResult;
import cn.com.ejiakanghu.dto.Location;
import cn.com.ejiakanghu.dto.StoreHomeInfo;
import cn.com.ejiakanghu.web.StoreController;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.db.domain.AgedmallStore;
import org.linlinjava.litemall.db.domain.LitemallCollect;
import org.linlinjava.litemall.db.domain.LitemallCollectExample;
import org.linlinjava.litemall.db.service.AgedmallStoreService;
import org.linlinjava.litemall.db.service.LitemallCollectService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.util.CollectConstant;
import org.linlinjava.litemall.db.util.GoodsConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户店铺服务
 */
@Component
public class StoreManager {
    private final Log logger = LogFactory.getLog(StoreManager.class);

    @Autowired
    private AgedmallStoreService agedmallStoreService;
    @Autowired
    private LitemallCollectService collectService;
    @Autowired
    private LitemallGoodsService goodsService;

    public BaseResult home(Integer userId, Integer id) {
        AgedmallStore agedmallStore = agedmallStoreService.findStoreById(id);
        StoreHomeInfo storeHomeInfo = StoreHomeInfo.getInstance(agedmallStore);
        // 获取上架售卖和租赁商品数
        Location location = LocationContext.get();
        int saleGoodsCount = goodsService.countByStoreId(agedmallStore.getId().intValue(), GoodsConstant.GOODS_TYPE_SALE);
        int leaseGoodsCount = goodsService.countByStoreId(agedmallStore.getId().intValue(), GoodsConstant.GOODS_TYPE_LEASE, location.getProvince(), location.getCity());
        storeHomeInfo.setSaleGoodsCount(saleGoodsCount);
        storeHomeInfo.setLeaseGoodsCount(leaseGoodsCount);
        // 是否收藏
        if (userId == null) {
            storeHomeInfo.setIsCollect(0);
        } else {
            int isCollect = collectService.isCollect(userId, id, CollectConstant.TYPE_STORE);
            storeHomeInfo.setIsCollect(isCollect);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("store", storeHomeInfo);
        return RespUtil.success(data);
    }

}